<?php
namespace app\pay\controller;
use think\Controller;
use think\Db;
use app\pay\model\User;
/**
 * 分润类
 * Class Profit
 * @package app\pay\controller
 */
class Profit extends Controller
{
	public function index()
	{
		//return $this->payrecords("KJ19051405101877834044110");
	}

	/**
	 * 扣款分润
	 * @param string $form_no
	 */
	public function repayment($form_no = '')
	{
		if (empty($form_no)) {
			return ['error' => 1, 'msg' => '订单错误'];
		}
		$orderInfo = Db::name('plan')->where(['plan_form_no' => $form_no])->find();
		if ($orderInfo["plan_type"] != '2') {
			return '单号不存在';
		}
		if ($orderInfo["plan_state"] != '1') {
			return '单号不存在';
		}
		//获取用户信息
		$user = Db::name('user')->where(['user_id' => $orderInfo['plan_uid']])->field("user_id,user_pid,user_type_id,user_name")->find();
		if (empty($user)) {
			return '用户信息错误';
		}

		/******************************开始会员包含上级分润***********************************/
		$userModel = new User;
		$userlist = $userModel->where(["user_state" => 0])->field("user_id,user_pid,user_type_id,user_name")->order("user_id asc")->select();

		//先判断自己是什么等级
		if ($user["user_type_id"] <= 2) {
			//先找到合伙人
			$list = $userModel->getGaoLev($userlist, $user["user_pid"], 3);
			$hhrUserInfo = $list[0];
			if (!empty($hhrUserInfo)) {
				$this->doFan($user["user_type_id"], $hhrUserInfo["user_type_id"], $hhrUserInfo["user_id"], $orderInfo);
				//找运营中心上级
				$list = $userModel->getGaoLev($userlist, $user["user_pid"], 4);
				$yyzxUserInfo = $list[0];
				if (!empty($yyzxUserInfo)) {
					$this->doFan($hhrUserInfo["user_type_id"], $yyzxUserInfo["user_type_id"], $yyzxUserInfo["user_id"], $orderInfo);
				}
			} else {
				//找运营中心上级
				$list = $userModel->getGaoLev($userlist, $user["user_pid"], 4);
				$yyzxUserInfo = $list[0];
				if (!empty($yyzxUserInfo)) {
					$this->doFan($user["user_type_id"], $yyzxUserInfo["user_type_id"], $yyzxUserInfo["user_id"], $orderInfo);
				}
			}
		} elseif ($user["user_type_id"] == '3') {//合伙人
			//先给自己返
			//$this->doFan(1, $user["user_type_id"], $user["user_id"], $orderInfo);
			//找运营中心上级
			$list = $userModel->getGaoLev($userlist, $user["user_pid"], 4);
			$yyzxUserInfo = $list[0];
			if (!empty($yyzxUserInfo)) {
				$this->doFan($user["user_type_id"], $yyzxUserInfo["user_type_id"], $yyzxUserInfo["user_id"], $orderInfo);
			}
		} elseif ($user["user_type_id"] == '4') {//运营中心
			//先给自己返
			//$this->doFan(1, $user["user_type_id"], $user["user_id"], $orderInfo);
		}
		/******************************开始上级会员分润***********************************/
	}


	/**
	 * 快捷分润
	 * @param string $form_no
	 */
	public function payrecords($records_form_no = '')
	{
		if (empty($records_form_no)) {
			return ['error' => 1, 'msg' => '订单错误'];
		}
		$orderInfo = Db::name('pay_records')->where(['records_form_no' => $records_form_no])->find();
		if ($orderInfo["records_state"] != '1') {
			return '单号不存在';
		}
		//获取用户信息
		$user = Db::name('user')->where(['user_id' => $orderInfo['records_uid']])->field("user_id,user_pid,user_type_id,user_name")->find();
		if (empty($user)) {
			return '用户信息错误';
		}

		/******************************开始会员包含上级分润***********************************/
		$userModel = new User;
		$userlist = $userModel->where(["user_state" => 0])->field("user_id,user_pid,user_type_id,user_name")->order("user_id asc")->select();

		//先判断自己是什么等级
		if ($user["user_type_id"] <= 2) {
			//先找到合伙人
			$list = $userModel->getGaoLev($userlist, $user["user_pid"], 3);
			$hhrUserInfo = $list[0];
			if (!empty($hhrUserInfo)) {
				$this->doFan($user["user_type_id"], $hhrUserInfo["user_type_id"], $hhrUserInfo["user_id"], $orderInfo,1);
				//找运营中心上级
				$list = $userModel->getGaoLev($userlist, $user["user_pid"], 4);
				$yyzxUserInfo = $list[0];
				if (!empty($yyzxUserInfo)) {
					$this->doFan($hhrUserInfo["user_type_id"], $yyzxUserInfo["user_type_id"], $yyzxUserInfo["user_id"], $orderInfo,1);
				}
			} else {
				//找运营中心上级
				$list = $userModel->getGaoLev($userlist, $user["user_pid"], 4);
				$yyzxUserInfo = $list[0];
				if (!empty($yyzxUserInfo)) {
					$this->doFan($user["user_type_id"], $yyzxUserInfo["user_type_id"], $yyzxUserInfo["user_id"], $orderInfo,1);
				}
			}
		} elseif ($user["user_type_id"] == '3') {//合伙人
			//先给自己返
			//$this->doFan(1, $user["user_type_id"], $user["user_id"], $orderInfo);
			//找运营中心上级
			$list = $userModel->getGaoLev($userlist, $user["user_pid"], 4);
			$yyzxUserInfo = $list[0];
			if (!empty($yyzxUserInfo)) {
				$this->doFan($user["user_type_id"], $yyzxUserInfo["user_type_id"], $yyzxUserInfo["user_id"], $orderInfo,1);
			}
		} elseif ($user["user_type_id"] == '4') {//运营中心
			//先给自己返
			//$this->doFan(1, $user["user_type_id"], $user["user_id"], $orderInfo);
		}
		/******************************开始上级会员分润***********************************/
	}


	/**
	 * @param        $user_type_id1                【当前用户级别】
	 * @param        $user_type_id2                【上级用户级别】
	 * @param        $pid                        【分润收益人id】
	 * @param        $orderInfo                    【订单信息】
	 * @param string $type 【1款姐2扣款】
	 *
	 */
	private function doFan($user_type_id1, $user_type_id2, $pid, $orderInfo, $type = '2')
	{
		$userModel = new User;
		if($type=='1'){//快捷收款
			$money = $orderInfo["records_money"];
			$plan_form_no = $orderInfo["records_form_no"];
			$rate1 = $userModel->getUserPayrecordsRankConfig($user_type_id1);
			$rate2 = $userModel->getUserPayrecordsRankConfig($user_type_id2);
			$trading_type = 2;
			$blog_type = 2;
			$touser=$orderInfo["records_uid"];
			$desc = '收款：' . $money . '元分润';
		}elseif($type=='2'){//代还扣款
			$money = $orderInfo["plan_money"];
			$plan_form_no = $orderInfo["plan_form_no"];
			$rate1 = $userModel->getUserRankConfig($user_type_id1);
			$rate2 = $userModel->getUserRankConfig($user_type_id2);
			$trading_type = 1;
			$blog_type = 3;
			$touser=$orderInfo["plan_uid"];
			$desc = '还款：' . $money . '元分润';
		}

		$rate = $rate1 - $rate2;
		$profit_money = blxsd2($money * $rate, 2);
//		dump($rate);
//		dump($profit_money);
//		dump($pid);
//		dump($orderInfo);
		if ($profit_money <= 0) {
			return "分润金额小于0";
		}
		//return "截断";
		$tradingInfo = Db::name('trading')->where(["trading_form_no" => $plan_form_no, "trading_uid" => $pid])->find();
		if (empty($tradingInfo)) {
			//我的交易记录
			$data['trading_uid'] = $pid;
			$data['trading_title'] = '扣款分润';
			$data['trading_type'] = $trading_type;
			$data['trading_orderid'] = $orderInfo['plan_id'];
			$data['trading_form_no'] = $plan_form_no;
			$data['trading_money'] = $profit_money;
			$data['trading_time'] = time();
			Db::name('trading')->insert($data);
			bonuslog($pid, $profit_money, time(), $blog_type, $desc, 0, $touser, 0, $plan_form_no);
		}
	}
}
